<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>restfull</title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="well">
    <h3>需要引入的框架</h3>
    <xmp>
        $ cnpm install express --save
        $ cnpm install body-parser --save
    </xmp>
    <h3>get根据id获取</h3>
    <xmp>
app.get('/user/:id',function (req,res) {
    fs.readFile(__dirname+"/"+"users.json",'utf-8',function (err,data) {
        data=JSON.parse(data);
        var user=data["user"+req.params.id];
        if(user){
            res.send(JSON.stringify(user));
        }else{
            res.send('用户不存在!')
        }
    })
});
    </xmp>
    <h3>post增加用户</h3>
    <xmp>
app.post('/user',function (req,res) {
    console.log('Hello Post');
    fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) {
        data = JSON.parse( data );
        data["user4"] = user["user4"];
        console.log( data );
        res.end( JSON.stringify(data));
    });
});
    </xmp>
    <h3>根据id删除用户</h3>
    <xmp>
app.delete('/user/:id',function (req,res) {
    res.writeHead(200,{"Content-Type":"text/plain;charset=utf-8"});
    res.end('删除成功：'+req.params.id);
});
    </xmp>
    <h3>修改用户</h3>
    <xmp>
app.put('/user/:id',function (req,res) {
    res.writeHead(200,{"Content-Type":"text/plain;charset=utf-8"});
    res.end('更新成功：'+req.params.id);
});
    </xmp>
</div>
</body>
</html>